package com.example.SBootDemo.service.Imp.function;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.example.SBootDemo.Dao.Interface.funDao;
import com.example.SBootDemo.Dao.Interface.roleDao;
import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.Util.TableTitles;
import com.example.SBootDemo.Util.funplace;
import com.example.SBootDemo.model.function.function;
import com.example.SBootDemo.model.function.role;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.funinterface;
@Service
public class funimpl implements funinterface {

	@Autowired
	funDao funs;
	@Autowired
	roleDao role;
	@Override
	public int getFuns(int pageinfo, HttpServletRequest request) throws Myexception {
		// TODO Auto-generated method stub
		int flag=0;
		
		Sort sort = new Sort(Direction.DESC, "id");
	    Pageable pageable = new PageRequest(pageinfo, TableTitles.getPagesize(), sort);
	    Page page = null;
		
			try {
				page = funs.findAll(pageable);
				flag=1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new Myexception("分页查询fun方法出错");
			}
			 
		    //面包屑导航
	    funplace funplace = new funplace("系统管理","用户与权限","系统功能管理-功能列表");
	    List<String> titles = TableTitles.getFunTitles();
	    //列表信息
		 request.setAttribute("List", page.getContent());
		//列表栏目信息
		 request.setAttribute("titles", titles);
		 //分页总页数
		 request.setAttribute("pages", page.getTotalPages());
		 //当前页，spring date jpa默认从0算起
		 request.setAttribute("number", page.getNumber());
	   
		 request.setAttribute("funplace", funplace);
		return  flag;
	}
	
	/*************************跳转到添加页面**************************************************************/
	
	@Override
	public void addpage(HttpServletRequest request) {
		// TODO Auto-generated method stub
		funplace funplace = new funplace("系统管理","用户与权限","系统功能管理-功能添加");
		  request.setAttribute("funplace", funplace);
		  List<function> fathers=funs.findByPid(0);
		  function fun= new function();
		  fun.setPid(-1);
		  //空user 前台模板取null有用
		  request.setAttribute("fun", fun);
		  //前段父功能下拉
		  request.setAttribute("fathers", fathers);
	}
	/*************************跳转到修改功能页面**************************************************************/
	@Override
	public void updatepage(HttpServletRequest request, int id) {
		// TODO Auto-generated method stub
		 function fun = funs.findById(id);
		 funplace funplace = new funplace("系统管理","用户与权限","系统功能管理-功能修改");
		 List<function> fathers=funs.findByPid(0);
		 request.setAttribute("fathers", fathers);
		 request.setAttribute("fun", fun);
	     request.setAttribute("funplace", funplace);
		 
	}
	/*************************新功能添加以及功能修改**************************************************************/
	@Override
	public void updateFun(function function, HttpServletRequest request) throws Myexception {
		// TODO Auto-generated method stub
		
		try {
			funs.save(function);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("更新&添加功能信息方法出错");
		}
	}
	/*************************功能删除**************************************************************/
	@Override
	@Transactional
	public void deleteFun(List<Integer> listid) throws Myexception {
		// TODO Auto-generated method stub
		try {
			for(int i=0;i<listid.size();i++){
				funs.deleteById(listid.get(i));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("删除功能出错");
		}
	}
	/*************************初始shiro当前用户当前点击模块侧边栏功能*************************************************************/
	@Override
	public void getSystemFun(String id,HttpServletRequest request) throws Myexception {
		// TODO Auto-generated method stub
		
		/**
		 * 从shiro中得到的user.role.funlist信息在过滤时会将shiro中信息也同步删除
		 * 
		 * 
		 */
		Subject currentUser = SecurityUtils.getSubject();
		user user = (user) currentUser.getPrincipal();
		List<function> functions1=new ArrayList<>();
		
		if(user.getRole().getIfadmin()==1){
			 //user.getRole().setFunlist(funs.findAll());
			 functions1.addAll(funs.findAll());
			 //System.out.println("1111111111"+user.getRole().getFunlist().size());
			 
		}else{
			 functions1.addAll((role.findById(user.getRole().getId())).getFunlist());
			 //System.out.println("2222222"+functions1.size());
		}
		
		
		System.out.println(functions1.size());
		
		Iterator<function> it = functions1.iterator();
		List<function> functions=new ArrayList<>();
		while(it.hasNext()){
			function fun=it.next();
		   if(!fun.getModulename().equals(id)){
			   
			    it.remove();
		   }else{
			   functions.add(fun);
		   }
		    
		}
		//System.out.println(functions.toString());
		 request.getSession().setAttribute("functions",functions);
	}

}
